package com.ljh.ad.consumer;

import com.alibaba.fastjson.JSON;
import com.ljh.ad.dto.MySqlRowData;
import com.ljh.ad.sender.ISender;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author ljh
 * @create 2020/6/19
 * @describe
 */
@Component
@Slf4j
public class BinlogConsumer {


    @Autowired
    private ISender sender;

    @KafkaListener(topics = "imooc", groupId = "ad-search")
    public void processMysqlRowData(ConsumerRecord<?, ?> record) {

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            MySqlRowData rowData = JSON.parseObject(
                    message.toString(),
                    MySqlRowData.class
            );
            log.info("kafka processMysqlRowData: {}", JSON.toJSONString(rowData));
            sender.sender(rowData);
        }
    }

    @KafkaListener(groupId = "group1",topics ="first")
    public void processMysqlRowData2(ConsumerRecord<?, ?> record) {

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            log.info("2 get msg {}",message);
        }
    }
}
